﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Controls;
using HT.PipeLine.UIFuncBase;
using HT.PipeLine.UIFuncDataMana.ControlEvents;
using HT.PipeLine.UIFuncDataMana.Functions;
using HT.PipeLine.UIFuncDataMana.Utils;
using System.Threading;
using HT.PipeLine.UIFuncDataMana.Functions.ToolStripMenu;
using HT.PipeLine.UIFuncDataMana.FunctionManager;
using HT.PipeLine.UIFuncDataMana.Functions.QueryStatatistic;

namespace HT.PipeLine.DataMana
{
    public partial class main : DevComponents.DotNetBar.Office2007RibbonForm
    {
        MethodManager methodManager = null;
        FunctionManager functionManager = null;
        Frm_Start startFrm = new Frm_Start();
        Thread thread = null;

        public main()
        {
            this.Visible = false;

            List<Type> methods = new List<Type>
            {
            typeof(CreateNewMxd), typeof(IndexTreeConfigSetting), typeof(OpenNewMxd), typeof(SaveCurrentMxd), 
            typeof(FeatureEditingAddFeature),typeof(FeatureEditingAddField), typeof(FeatureEditingChangeEdit), typeof(FeatureEditingDeleteEdit), 
            typeof(FeatureEditingRedoEdit), typeof(FeatureEditingSaveEdit), typeof(FeatureEditingSelectEdit), typeof(FeatureEditingStartEdit), 
            typeof(FeatureEditingStopEdit), typeof(FeatureEditingUndoEdit), typeof(MapClearRender), typeof(MapCursor), typeof(MapFullExtent), 
            typeof(MapGlass), typeof(MapMeasure), typeof(MapNextView), typeof(MapPan), typeof(MapPreView), typeof(MapSelection), typeof(MapZoomIn), 
            typeof(MapZoomOut), typeof(LayOutAddTemplate), typeof(LayOutAnnotation), typeof(LayOutBoaderAndShade),typeof(LayOutEditText),
            typeof(LayOutFullExtent), typeof(LayOutInsertLegend),typeof(LayOutInsertNorthArrow), typeof(LayOutInsertScale),
            typeof(LayOutInsertScaleDetails), typeof(LayOutInsertText), typeof(LayOutNextView), typeof(LayOutPan),typeof(LayOutPreView),
            typeof(LayOutPrintAndExport), typeof(LayOutPrinttingSetting), typeof(LayOutSave), typeof(LayOutScreenPrint),typeof(LayOutSelectFeature),
            typeof(LayOutTurnMap),typeof(LayOutZoomIn),typeof(LayOutZoomOut), typeof(DataActionAddToMapLayer), typeof(DataActionEditOriginSource), 
            typeof(DataActionExport),typeof(DataActionRefresh),typeof(DataActionEditAtrribute),typeof(DataActionEditAtrributeMenu),typeof(DataActionExportMenu),typeof(DataActionShowOrigonSource),
            typeof(GroupLayerActionAddLayer),typeof(GroupLayerActionCloseAllLaer),typeof(GroupLayerActionCollapseAllLayer),
            typeof(GroupLayerActionExpandAllLayer),typeof(GroupLayerActionOpenAllLayer),typeof(MapActionAddAnnotation),
            typeof(MapActionClearAnnotation),typeof(MapActionClearScales),typeof(MapActionClearSelection),typeof(MapActionCreateSelectLayer),
            typeof(MapActionExportLayer),typeof(MapActionLoadResult),typeof(MapActionMarrySetting),typeof(MapActionRelateTable),
            typeof(MapActionRemoveLayer),typeof(MapActionSelectAllFeatures),typeof(MapActionSetMaxScale),typeof(MapActionSetMiniScale),
            typeof(MapActionShowAttribute),typeof(MapActionTurnSelection),typeof(MapActionZoomToLayer),typeof(MapActionZoomToSelectFeature),
            typeof(About),typeof(DataBaseConnecting),typeof(SdeConnecting),typeof(Identify),typeof(RectangleQuery),typeof(ConditionQuery),
            typeof(MapActionIdentify),typeof(StripMenuRefresh),typeof(QueryByCZ),typeof(QueryByQSDW),typeof(PipeLengthStat),typeof(PipeCountStat),
            };

            //在初始化控件前实例化methodManager，因为控件的click事件要调用methodManager的方法
            InitializeComponent();

            #region 初始化全局参数
            Extern.advTree = this.advTree1;
            Extern.axMapControl地图预览 = this.axMapControl地图预览;
            Extern.axMapControl数据预览 = this.axMapControl数据预览;
            Extern.axPageLayoutControl = this.axPageLayoutControl1;
            Extern.axTOCControl1 = this.axTOCControl1;
            Extern.dataGridViewX1 = this.dataGridViewX1;
            Extern.FeatureEditingSaveEdit = this.FeatureEditingSaveEdit;
            Extern.FeatureEditingStopEdit = this.FeatureEditingStopEdit;
            Extern.folderBrowserDialog1 = this.folderBrowserDialog1;
            Extern.mainFrm = this;
            Extern.labellocaltion = this.labellocaltion;
            Extern.openFileDialog = this.openFileDialog1;
            Extern.openFileMXT = this.openFileMXT;
            Extern.ribbonControl1 = this.ribbonControl1;
            Extern.ribbonTabItem数据管理 = this.ribbonTabItem数据管理;
            Extern.ribbonTabItem地图操作 = this.ribbonTabItem地图管理;
            Extern.ribbonTabItem制图打印 = this.ribbonTabItem制图打印;
            Extern.uctooltrip = this.uctooltrip1;
            Extern.pnl_ViewData = this.pnl_ViewData;
            Extern.pnl_ViewLayout = this.pnl_ViewLayout;
            Extern.pnl_ViewMap = this.pnl_ViewMap;
            Extern.pnl_ManData = this.pnl_ManData;
            Extern.pnl_ManMap = this.pnl_ManMap;
            Extern.txt_Scale = this.txt_Scale;
            Extern.OpenRecentMXD = this.OpenRecentMXD;
            Extern.form_About = new Form_about();
            Extern.startupWin = new StartupWin(axMapControl地图预览);
            Extern.contextMenuStripLayer = this.contextMenuStripLayer;
            Extern.contextMenuStripTOC = this.contextMenuStripTOC;
            Extern.advTree1 = this.advTree1;
            Extern.toolTip1 = this.toolTip1;
            Extern.Cursor = this.Cursor;

            Extern.GZKDataSetName = ConfigHelper.GetConfigString("GZKDataSetName");
            Extern.XZKDataSetName = ConfigHelper.GetConfigString("XZKDataSetName");
            #endregion

            methodManager = new MethodManager(this, methods);
            functionManager = new FunctionManager();

            this.Text = ConfigHelper.GetConfigString("name");

            if (ResourceManager.DAhelper.ini() == false)
            {
                MessageBoxEx.Show("请配置数据库连接!");
            }
            this.StartPosition = FormStartPosition.Manual;
            this.Size = Screen.GetWorkingArea(this).Size;

            MainFormOperation.ini();

            thread = new Thread(new ThreadStart(ShowLog));

            thread.Start();

            //默认打开的地图
           // UIFuncDataMana.Functions.OpenNewMxd.DefaultRun(@"D:\01工作\020管线数据\00丽水管线数据\丽水综合管线数据\PIPELINEForCS.mxd");

            //设置允许控件跨线程访问
            Form.CheckForIllegalCrossThreadCalls = false;

            while (startFrm.DialogResult != DialogResult.OK)
            {
                Application.DoEvents();
            }

            //加载最近打开的地图
            ShowRecentOpen.Show();

            thread.Join();
        }

        private void ShowLog()
        {
            startFrm.ShowDialog();
        }

        private void main_Load(object sender, EventArgs e)
        {
            //初始化时显示管线入库菜单、左侧TAB显示管线入库，管线入库TAB中显示原始库的树，显示数据地图的panel
            this.ribbonControl1.SelectedRibbonTabItem = this.ribbonTabItem地图管理;

            Extern.axMapControl地图预览 = this.axMapControl地图预览;
            MainFormOperation.SwitchPanel("pnl_ViewMap");

            
        }

        private void main_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();
            this.Close();
        }

        //这样做是为了能够显示Main的视图设计器
        public void MethodButton_Click(object sender, EventArgs e)
        {
            methodManager.MethodButton_Click(sender, e);
        }

        public void FunctionButton_Click(object sender, EventArgs e)
        {
            functionManager.FunctionButton_Click(sender, e);
        }

        #region 控件的其它事件
        /// <summary>
        /// 地图管理--图层树双击事件，双击图例进行符号设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axTOCControl1_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e)
        {
            SetSymbol.Run(e);
        }

        /// <summary>
        /// 地图管理--图层数点击事件，左键：选中图层？右键：弹出菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
        {
            ClickLayer.Run(e);
        }

        /// <summary>
        /// 数据管理图层树鼠标提示信息，显示节点对应数据信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void advTree1_MouseMove(object sender, MouseEventArgs e)
        {
            //ShowInfoOfNode.Run(sender, e);
        }

        /// <summary>
        /// 数据管理--图层树，选中节点进行预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            ScanDataOfNode.Run();
        }


        private void dataGridViewX1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataGridViewX1.EndEdit();
        }

        private void dataGridViewX1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if (dataGridViewX1.Tag.ToString() == "0")
                {
                    for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
                    {
                        dataGridViewX1.Rows[i].Cells[0].Value = 1;
                    }
                    dataGridViewX1.Tag = 1;
                }
                else
                {
                    for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
                    {
                        dataGridViewX1.Rows[i].Cells[0].Value = 0;
                    }
                    dataGridViewX1.Tag = 0;
                }

            }
        }

        private void axMapControl地图预览_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            txt_Scale.Text = "1 : " + Math.Round(axMapControl地图预览.MapScale, 4).ToString();
            statusBarScale.Text = "  比例尺 1 : " + Math.Round(axMapControl地图预览.MapScale, 4).ToString();
        }

        private void axMapControl数据预览_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            //txt_Scale.Text = "1 : " + Math.Round(axMapControl数据预览.MapScale, 4).ToString();
        }

        private void axMapControl地图预览_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            statusBarXY.Text = "X:" + Math.Round(e.mapX, 2).ToString() + "  Y:" + Math.Round(e.mapY, 2).ToString();
        }

        private void axMapControl数据预览_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            axTOCControl1.SetBuddyControl(axMapControl数据预览.Object);
            axTOCControl1.Update();
        }

        private void axMapControl地图预览_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            axTOCControl1.SetBuddyControl(axMapControl地图预览.Object);
            axTOCControl1.Update();
        }
     

        private void ribbonControl1_SelectedRibbonTabChanged(object sender, EventArgs e)
        {
            if (this.ribbonControl1.SelectedRibbonTabItem == this.ribbonTabItem数据管理)
            {
                MainFormOperation.SwitchPanel("pnl_ViewData");
            }
            else if (this.ribbonControl1.SelectedRibbonTabItem == this.ribbonTabItem制图打印)
            {
                MainFormOperation.SwitchPanel("pnl_ViewLayout");
                MainFormOperation.reini();
            }
            else
            {
                MainFormOperation.SwitchPanel("pnl_ViewMap");
            }
        }

        private void txt_Scale_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData==Keys.Enter)
            {
                if (this.axMapControl地图预览 != null)
                {
                    try
                    {
                        string scale = txt_Scale.Text.Replace(" ", "");
                        scale = scale.Replace(",", "");
                        scale = scale.Substring(2, scale.Length - 2);

                        this.axMapControl地图预览.MapScale = Convert.ToDouble(scale);
                        this.axMapControl地图预览.Refresh();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("比例尺格式错误！", "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
        }

        private void sbtn_IsMapOperationShow_ValueChanged(object sender, EventArgs e)
        {
            if (sbtn_IsMapOperationShow.Value)
            {
                this.rbnBar_MapOperation.Visible = false;
            }
            else
            {
                this.rbnBar_MapOperation.Visible = true;
            }
        }

        #endregion

        private void ribbonTabItem数据管理_Click(object sender, EventArgs e)
        {

        }
    }
}
